public class Solution {
    public String longestCommonPrefix(String[] strs) {
        //初始化
        String ans = strs[0];
        for (int i = 1; i < strs.length; i++) {
            ans = returnCommStr(ans, strs[i]);
        }
        return ans;
    }

    /**
     * 求两个数的最长前缀
     */
    public String returnCommStr(String str1, String str2) {

        //公共前缀
        String comStr = "";
        for (int i = 0; i < str1.length(); i++) {

            //判断i < str2.length()是为了防止两str长度不一致str2数组下标越界
            if (i < str2.length()) {
                //若直接遇到不相同也直接返回
                if (str1.charAt(i) != str2.charAt(i)) {
                    return comStr;
                }
                //若相同直接拼接。
                comStr = comStr + str1.charAt(i);
                continue;
            }
        }
        return comStr;
    }


}
